home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 023 / ver30 / window.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  10KB  |  418 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *        Window handling.
  4.  * Version:    29
  5.  * Last edit:    10-Feb-86
  6.  * By:        rex::conroy
  7.  *        decvax!decwrl!dec-rhea!dec-rex!conroy
  8.  */
  9. #include    "def.h"
  10.  
  11. /*
  12.  * Reposition dot in the current
  13.  * window to line "n". If the argument is
  14.  * positive, it is that line. If it is negative it
  15.  * is that line from the bottom. If it is 0 the window
  16.  * is centered (this is what the standard redisplay code
  17.  * does). With no argument it defaults to 1.
  18.  * Because of the default, it works like in
  19.  * Gosling.
  20.  */
  21. reposition(f, n, k)
  22. {
  23.     curwp->w_force = n;
  24.     curwp->w_flag |= WFFORCE;
  25.     return (TRUE);
  26. }
  27.  
  28. /*
  29.  * Refresh the display. A call is made to the
  30.  * "ttresize" entry in the terminal handler, which tries
  31.  * to reset "nrow" and "ncol". They will, however, never
  32.  * be set outside of the NROW or NCOL range. If the display
  33.  * changed size, arrange that everything is redone, then
  34.  * call "update" to fix the display. We do this so the
  35.  * new size can be displayed. In the normal case the
  36.  * call to "update" in "main.c" refreshes the screen,
  37.  * and all of the windows need not be recomputed.
  38.  * Note that when you get to the "display unusable"
  39.  * message, the screen will be messed up. If you make
  40.  * the window bigger again, and send another command,
  41.  * everything will get fixed!
  42.  */
  43. refresh(f, n, k)
  44. {
  45.     register WINDOW    *wp;
  46.     register int    oldnrow;
  47.     register int    oldncol;
  48.  
  49.     oldnrow = nrow;
  50.     oldncol = ncol;
  51.     ttresize();
  52.     if (nrow!=oldnrow || ncol!=oldncol) {
  53.         wp = wheadp;            /* Find last.        */
  54.         while (wp->w_wndp != NULL)
  55.             wp = wp->w_wndp;
  56.         if (nrow < wp->w_toprow+3) {    /* Check if too small.    */
  57.             eprintf("Display unusable");
  58.             return (FALSE);
  59.         }        
  60.         wp->w_ntrows = nrow-wp->w_toprow-2;
  61.         wp = wheadp;            /* Redraw all.        */
  62.         while (wp != NULL) {
  63.             wp->w_flag |= WFMODE|WFHARD;
  64.             wp = wp->w_wndp;
  65.         }
  66.         sgarbf = TRUE;
  67.         update();
  68.         eprintf("[New size %d by %d]", nrow, ncol);
  69.     } else
  70.         sgarbf = TRUE;
  71.     return (TRUE);
  72. }
  73.  
  74. /*
  75.  * The command make the next
  76.  * window (next => down the screen)
  77.  * the current window. There are no real
  78.  * errors, although the command does
  79.  * nothing if there is only 1 window on
  80.  * the screen.
  81.  */
  82. nextwind(f, n, k)
  83. {
  84.     register WINDOW    *wp;
  85.  
  86.     if ((wp=curwp->w_wndp) == NULL)
  87.         wp = wheadp;
  88.     curwp = wp;
  89.     curbp = wp->w_bufp;
  90.     return (TRUE);
  91. }
  92.  
  93. /*
  94.  * This command makes the previous
  95.  * window (previous => up the screen) the
  96.  * current window. There arn't any errors,
  97.  * although the command does not do a lot
  98.  * if there is 1 window.
  99.  */
  100. prevwind(f, n, k)
  101. {
  102.     register WINDOW    *wp1;
  103.     register WINDOW    *wp2;
  104.  
  105.     wp1 = wheadp;
  106.     wp2 = curwp;
  107.     if (wp1 == wp2)
  108.         wp2 = NULL;
  109.     while (wp1->w_wndp != wp2)
  110.         wp1 = wp1->w_wndp;
  111.     curwp = wp1;
  112.     curbp = wp1->w_bufp;
  113.     return (TRUE);
  114. }
  115.  
  116. /*
  117.  * This command moves the current
  118.  * window down by "arg" lines. Recompute
  119.  * the top line in the window. The move up and
  120.  * move down code is almost completely the same;
  121.  * most of the work has to do with reframing the
  122.  * window, and picking a new dot. We share the
  123.  * code by having "move down" just be an interface
  124.  * to "move up".
  125.  */
  126. mvdnwind(f, n, k)
  127. register int    n;
  128. {
  129.     return (mvupwind(f, -n, KRANDOM));
  130. }
  131.  
  132. /*
  133.  * Move the current window up by "arg"
  134.  * lines. Recompute the new top line of the window.
  135.  * Look to see if "." is still on the screen. If it is,
  136.  * you win. If it isn't, then move "." to center it
  137.  * in the new framing of the window (this command does
  138.  * not really move "."; it moves the frame).
  139.  */
  140. mvupwind(f, n, k)
  141. register int    n;
  142. {
  143.     register LINE    *lp;
  144.     register int    i;
  145.  
  146.     lp = curwp->w_linep;
  147.     if (n < 0) {
  148.         while (n++ && lp!=curbp->b_linep)
  149.             lp = lforw(lp);
  150.     } else {
  151.         while (n-- && lback(lp)!=curbp->b_linep)
  152.             lp = lback(lp);
  153.     }
  154.     curwp->w_linep = lp;
  155.     curwp->w_flag |= WFHARD;        /* Mode line is OK.    */
  156.     for (i=0; i<curwp->w_ntrows; ++i) {
  157.         if (lp == curwp->w_dotp)
  158.             return (TRUE);
  159.         if (lp == curbp->b_linep)
  160.             break;
  161.         lp = lforw(lp);
  162.     }
  163.     lp = curwp->w_linep;
  164.     i  = curwp->w_ntrows/2;
  165.     while (i-- && lp!=curbp->b_linep)
  166.         lp = lforw(lp);
  167.     curwp->w_dotp  = lp;
  168.     curwp->w_doto  = 0;
  169.     return (TRUE);
  170. }
  171.  
  172. /*
  173.  * This command makes the current
  174.  * window the only window on the screen.
  175.  * Try to set the framing
  176.  * so that "." does not have to move on
  177.  * the display. Some care has to be taken
  178.  * to keep the values of dot and mark
  179.  * in the buffer structures right if the
  180.  * distruction of a window makes a buffer
  181.  * become undisplayed.
  182.  */
  183. onlywind(f, n, k)
  184. {
  185.     register WINDOW    *wp;
  186.     register LINE    *lp;
  187.     register int    i;
  188.  
  189.     while (wheadp != curwp) {
  190.         wp = wheadp;
  191.         wheadp = wp->w_wndp;
  192.         if (--wp->w_bufp->b_nwnd == 0) {
  193.             wp->w_bufp->b_dotp  = wp->w_dotp;
  194.             wp->w_bufp->b_doto  = wp->w_doto;
  195.             wp->w_bufp->b_markp = wp->w_markp;
  196.             wp->w_bufp->b_marko = wp->w_marko;
  197.         }
  198.         free((char *) wp);
  199.     }
  200.     while (curwp->w_wndp != NULL) {
  201.         wp = curwp->w_wndp;
  202.         curwp->w_wndp = wp->w_wndp;
  203.         if (--wp->w_bufp->b_nwnd == 0) {
  204.             wp->w_bufp->b_dotp  = wp->w_dotp;
  205.             wp->w_bufp->b_doto  = wp->w_doto;
  206.             wp->w_bufp->b_markp = wp->w_markp;
  207.             wp->w_bufp->b_marko = wp->w_marko;
  208.         }
  209.         free((char *) wp);
  210.     }
  211.     lp = curwp->w_linep;
  212.     i  = curwp->w_toprow;
  213.     while (i!=0 && lback(lp)!=curbp->b_linep) {
  214.         --i;
  215.         lp = lback(lp);
  216.     }
  217.     curwp->w_toprow = 0;
  218.     curwp->w_ntrows = nrow-2;        /* 2 = mode, echo.    */
  219.     curwp->w_linep  = lp;
  220.     curwp->w_flag  |= WFMODE|WFHARD;
  221.     return (TRUE);
  222. }
  223.  
  224. /*
  225.  * Split the current window. A window
  226.  * smaller than 3 lines cannot be split.
  227.  * The only other error that is possible is
  228.  * a "malloc" failure allocating the structure
  229.  * for the new window.
  230.  */
  231. splitwind(f, n, k)
  232. {
  233.     register WINDOW    *wp;
  234.     register LINE    *lp;
  235.     register int    ntru;
  236.     register int    ntrl;
  237.     register int    ntrd;
  238.     register WINDOW    *wp1;
  239.     register WINDOW    *wp2;
  240.  
  241.     if (curwp->w_ntrows < 3) {
  242.         eprintf("Cannot split a %d line window", curwp->w_ntrows);
  243.         return (FALSE);
  244.     }
  245.     if ((wp = (WINDOW *) malloc(sizeof(WINDOW))) == NULL) {
  246.         eprintf("Cannot allocate WINDOW block");
  247.         return (FALSE);
  248.     }
  249.     ++curbp->b_nwnd;            /* Displayed twice.    */
  250.     wp->w_bufp  = curbp;
  251.     wp->w_dotp  = curwp->w_dotp;
  252.     wp->w_doto  = curwp->w_doto;
  253.     wp->w_markp = curwp->w_markp;
  254.     wp->w_marko = curwp->w_marko;
  255.     wp->w_flag  = 0;
  256.     wp->w_force = 0;
  257.     ntru = (curwp->w_ntrows-1) / 2;        /* Upper size        */
  258.     ntrl = (curwp->w_ntrows-1) - ntru;    /* Lower size        */
  259.     lp = curwp->w_linep;
  260.     ntrd = 0;
  261.     while (lp != curwp->w_dotp) {
  262.         ++ntrd;
  263.         lp = lforw(lp);
  264.     }
  265.     lp = curwp->w_linep;
  266.     if (ntrd <= ntru) {            /* Old is upper window.    */
  267.         if (ntrd == ntru)        /* Hit mode line.    */
  268.             lp = lforw(lp);
  269.         curwp->w_ntrows = ntru;
  270.         wp->w_wndp = curwp->w_wndp;
  271.         curwp->w_wndp = wp;
  272.         wp->w_toprow = curwp->w_toprow+ntru+1;
  273.         wp->w_ntrows = ntrl;
  274.     } else {                /* Old is lower window    */
  275.         wp1 = NULL;
  276.         wp2 = wheadp;
  277.         while (wp2 != curwp) {
  278.             wp1 = wp2;
  279.             wp2 = wp2->w_wndp;
  280.         }
  281.         if (wp1 == NULL)
  282.             wheadp = wp;
  283.         else
  284.             wp1->w_wndp = wp;
  285.         wp->w_wndp   = curwp;
  286.         wp->w_toprow = curwp->w_toprow;
  287.         wp->w_ntrows = ntru;
  288.         ++ntru;                /* Mode line.        */
  289.         curwp->w_toprow += ntru;
  290.         curwp->w_ntrows  = ntrl;
  291.         while (ntru--)
  292.             lp = lforw(lp);
  293.     }
  294.     curwp->w_linep = lp;            /* Adjust the top lines    */
  295.     wp->w_linep = lp;            /* if necessary.    */
  296.     curwp->w_flag |= WFMODE|WFHARD;
  297.     wp->w_flag |= WFMODE|WFHARD;
  298.     return (TRUE);
  299. }
  300.  
  301. /*
  302.  * Enlarge the current window.
  303.  * Find the window that loses space. Make
  304.  * sure it is big enough. If so, hack the window
  305.  * descriptions, and ask redisplay to do all the
  306.  * hard work. You don't just set "force reframe"
  307.  * because dot would move.
  308.  */
  309. enlargewind(f, n, k)
  310. {
  311.     register WINDOW    *adjwp;
  312.     register LINE    *lp;
  313.     register int    i;
  314.  
  315.     if (n < 0)
  316.         return (shrinkwind(f, -n, KRANDOM));
  317.     if (wheadp->w_wndp == NULL) {
  318.         eprintf("Only one window");
  319.         return (FALSE);
  320.     }
  321.     if ((adjwp=curwp->w_wndp) == NULL) {
  322.         adjwp = wheadp;
  323.         while (adjwp->w_wndp != curwp)
  324.             adjwp = adjwp->w_wndp;
  325.     }
  326.     if (adjwp->w_ntrows <= n) {
  327.         eprintf("Impossible change");
  328.         return (FALSE);
  329.     }
  330.     if (curwp->w_wndp == adjwp) {        /* Shrink below.    */
  331.         lp = adjwp->w_linep;
  332.         for (i=0; i<n && lp!=adjwp->w_bufp->b_linep; ++i)
  333.             lp = lforw(lp);
  334.         adjwp->w_linep  = lp;
  335.         adjwp->w_toprow += n;
  336.     } else {                /* Shrink above.    */
  337.         lp = curwp->w_linep;
  338.         for (i=0; i<n && lback(lp)!=curbp->b_linep; ++i)
  339.             lp = lback(lp);
  340.         curwp->w_linep  = lp;
  341.         curwp->w_toprow -= n;
  342.     }
  343.     curwp->w_ntrows += n;
  344.     adjwp->w_ntrows -= n;
  345.     curwp->w_flag |= WFMODE|WFHARD;
  346.     adjwp->w_flag |= WFMODE|WFHARD;
  347.     return (TRUE);
  348. }
  349.  
  350. /*
  351.  * Shrink the current window.
  352.  * Find the window that gains space. Hack at
  353.  * the window descriptions. Ask the redisplay to
  354.  * do all the hard work.
  355.  */
  356. shrinkwind(f, n, k)
  357. {
  358.     register WINDOW    *adjwp;
  359.     register LINE    *lp;
  360.     register int    i;
  361.  
  362.     if (n < 0)
  363.         return (enlargewind(f, -n, KRANDOM));
  364.     if (wheadp->w_wndp == NULL) {
  365.         eprintf("Only one window");
  366.         return (FALSE);
  367.     }
  368.     if ((adjwp=curwp->w_wndp) == NULL) {
  369.         adjwp = wheadp;
  370.         while (adjwp->w_wndp != curwp)
  371.             adjwp = adjwp->w_wndp;
  372.     }
  373.     if (curwp->w_ntrows <= n) {
  374.         eprintf("Impossible change");
  375.         return (FALSE);
  376.     }
  377.     if (curwp->w_wndp == adjwp) {        /* Grow below.        */
  378.         lp = adjwp->w_linep;
  379.         for (i=0; i<n && lback(lp)!=adjwp->w_bufp->b_linep; ++i)
  380.             lp = lback(lp);
  381.         adjwp->w_linep  = lp;
  382.         adjwp->w_toprow -= n;
  383.     } else {                /* Grow above.        */
  384.         lp = curwp->w_linep;
  385.         for (i=0; i<n && lp!=curbp->b_linep; ++i)
  386.             lp = lforw(lp);
  387.         curwp->w_linep  = lp;
  388.         curwp->w_toprow += n;
  389.     }
  390.     curwp->w_ntrows -= n;
  391.     adjwp->w_ntrows += n;
  392.     curwp->w_flag |= WFMODE|WFHARD;
  393.     adjwp->w_flag |= WFMODE|WFHARD;
  394.     return (TRUE);
  395. }
  396.  
  397. /*
  398.  * Pick a window for a pop-up.
  399.  * Split the screen if there is only
  400.  * one window. Pick the uppermost window that
  401.  * isn't the current window. An LRU algorithm
  402.  * might be better. Return a pointer, or
  403.  * NULL on error.
  404.  */
  405. WINDOW    *
  406. wpopup()
  407. {
  408.     register WINDOW    *wp;
  409.  
  410.     if (wheadp->w_wndp == NULL
  411.     && splitwind(FALSE, 0, KRANDOM) == FALSE)
  412.         return (NULL);
  413.     wp = wheadp;                /* Find window to use    */
  414.     while (wp!=NULL && wp==curwp)
  415.         wp = wp->w_wndp;
  416.     return (wp);
  417. }
  418.